const dbm = require('../config/dbModels');
const { decodeJWTId } = require('../core/jwt');
const {aseEncode}=require('../core/crypto')
class PasswordDao {
    /**
     * 修改登录者自己的密码
     * @param ctx 总数据
     * @returns {Promise<*>}
     */
    static async update(ctx) {
        var selfId = decodeJWTId(ctx)
        var UserInfo=await dbm.User.findOne({
            where: {
                id:selfId
            },
            raw:true,
            attributes:['password']
        })
        console.log('======UserInfo', UserInfo);
         // 接收客户端参数
        let req = ctx.request.body;
        var newPass = aseEncode(req.newpsw)
        var oldPass = aseEncode(req.oldpsw)
        var sqlOldPsw=await dbm.User.findOne({
            where: {
                id: selfId
            },
            raw:true,
            attributes:['password']
        })
        
        if (sqlOldPsw.password === oldPass) {
            return await dbm.User.update(
                { password: newPass },
                {
                    where: {
                        id: selfId
                    }
                }
            )
        } else { 
            throw new global.errs.ParameterException('旧密码验证不通过')
        }
    }
}
module.exports = PasswordDao
